package com.getpebble.android.bluetooth.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import com.getpebble.android.R;
import com.getpebble.android.bluetooth.radio.PebbleBluetoothAdapter.AdapterState;
import com.getpebble.android.common.core.trace.Trace;
import com.sun.mail.iap.Response;
import java.lang.ref.WeakReference;

public abstract class AdapterStateReceiver extends BroadcastReceiver {
    private static final String TAG = AdapterStateReceiver.class.getSimpleName();
    private WeakReference<Context> mContext = null;

    public abstract void onAdapterStateChanged(AdapterState adapterState);

    public AdapterStateReceiver(Context context) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
        context.registerReceiver(this, intentFilter);
        this.mContext = new WeakReference(context);
    }

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.bluetooth.adapter.action.STATE_CHANGED")) {
            int state = intent.getIntExtra("android.bluetooth.adapter.extra.STATE", Integer.MIN_VALUE);
            switch (state) {
                case Integer.MIN_VALUE:
                    onAdapterStateChanged(AdapterState.ERROR);
                    return;
                case R.styleable.WalletFragmentStyle_maskedWalletDetailsLogoImageType /*10*/:
                    onAdapterStateChanged(AdapterState.OFF);
                    return;
                case R.styleable.MapAttrs_uiZoomGestures /*11*/:
                    onAdapterStateChanged(AdapterState.TURNING_ON);
                    return;
                case Response.BAD /*12*/:
                    onAdapterStateChanged(AdapterState.ON);
                    return;
                case R.styleable.MapAttrs_zOrderOnTop /*13*/:
                    onAdapterStateChanged(AdapterState.TURNING_OFF);
                    return;
                default:
                    Trace.warning(TAG, "Unexpected AdapterState " + String.valueOf(state));
                    return;
            }
        }
    }

    public void stopReceiving() {
        Context context = (Context) this.mContext.get();
        if (context != null) {
            context.unregisterReceiver(this);
        }
    }
}
